package com.apps.taobao;

import com.apps.file.service.FileHelper;
import com.apps.ppt.utils.PPtxImagesHelper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.List;

/**
 * 处理淘宝上传PPT的业务
 */
@Slf4j
public class TaobaoPPtHelper {


    /**
     * 读取某文件夹下的所有PPT生成指定宽高的图片
     * @param path
     * @param width
     * @param height
     */
    @SneakyThrows
    public static void generatePPtImages(String path,int width,int height,String out_flod)
    {
        //扫描出所有pptx文件
        List<String> ppts= FileHelper.scanFilesWithNoRecursion(path,true,"pptx","ppt");
        log.info("共计扫描到"+ppts.size()+"个ppt文件");
        //创建文件存储文件夹
        File file=new File(path+"\\"+out_flod+"\\");
        if(!file.exists())
            file.mkdirs();
        for(String pptpath:ppts)
        {
            try {
                File ppt=new File(pptpath);
                BufferedImage image= PPtxImagesHelper.readPPtToImages(ppt,3);
                BufferedImage subimage=Thumbnails.of(image).width(width).asBufferedImage();
                if(subimage.getHeight() < height)
                    continue;
                ImageIO.write(subimage.getSubimage(0,0,width,height),"jpg",new File(file.getAbsolutePath()+"\\"+ppt.getName().split("\\.")[0]+".jpg"));
            }catch (Exception e)
            {
                e.printStackTrace();
                continue;
            }
        }
    }

    /**
     * 将某个文件夹下的jpg文件合成淘宝详情图
     * @param path
     * @param row
     * @param img_width
     * @param img_height
     */
    @SneakyThrows
   public static void generateTaobaoDetail(String path,int row,int img_width,int img_height,int part_height)
   {
       int row_space=2;
       //扫描出所有jpg文件
       List<String> ppts= FileHelper.scanFilesWithNoRecursion(path,false,"jpg");
       log.info("共计扫描到"+ppts.size()+"个jpg文件");
       //求出行数
       int len=ppts.size()%row==0?ppts.size():ppts.size()-1;
       //画板的高度和宽度
       int width=(img_width*row)+(row_space*(row-1));
       int height=((img_height)*(len/row))+(row_space*(len/row));
       //创建画板
       BufferedImage result=new BufferedImage(width,height , BufferedImage.TYPE_INT_RGB);
       Graphics2D graphics = result.createGraphics();
       graphics.setPaint(Color.white);
       graphics.fill(new Rectangle2D.Float(0, 0, width,height));
       for(int i=0;i<len;i++)
       {
           String jpg=ppts.get(i);
           File file=new File(jpg);
           if(!file.exists())
           {
               System.out.println(jpg+"不存在");
               continue;
           }
           BufferedImage bufImage = ImageIO.read(file);
           int x=(PPtxImagesHelper.calculationX(i,row))*((width/row)+(row-1)*row_space);
           int y=((i/row)*img_height)+((i/row)*row_space);
           Image imges=bufImage.getScaledInstance((width/row),img_height,Image.SCALE_DEFAULT);
           result.getGraphics().drawImage(imges,x,y,null);
       }
       //创建输出文件夹
       File detail=new File(new File(path).getParent()+"\\detail\\");
       if(!detail.exists())
           detail.mkdirs();
       int part_len=result.getHeight()/part_height;
       int i=0;
       String detail_name="detail_";
       for(;i<part_len;i++)
       {
           int y=i*part_height;
           ImageIO.write(result.getSubimage(0,y,width,part_height),"jpg",new File(detail.getAbsolutePath()+"\\"+detail_name+"_"+i+".jpg"));
       }
       //处理剩余图片
       float last_height=((float)result.getHeight()/(float)part_height)-part_len;
       ImageIO.write(result.getSubimage(0,part_len*part_height,width,(int)(part_height*last_height)),"jpg",new File(detail.getAbsolutePath()+"\\"+detail_name+"_"+i+".jpg"));

   }

    /**
     * 处理一个文件夹
     * @param path
     */
   public static void handlePPtFlod(String path)
   {
       //图片的宽度
       int img_width=499;
       //图片的高度
       int img_height=700;
       //切图高度,淘宝允许的单张图片高度,会按这个高度切成小图
       int part_height=2500;
       //一行拼接几张图片
       int row=3;
       //图片存储文件夹
       String out_flod="images";
       try {
           //给所有PPT生成图片
           generatePPtImages(path,img_width,img_height,out_flod);
       }catch (Exception e)
       {
           e.printStackTrace();
       }

       //生成淘宝所需图片
       generateTaobaoDetail(path+"\\"+out_flod+"\\",row,img_width,img_height,part_height);
   }


    public static void main(String... args) throws Exception {

       //handlePPtFlod("I:\\素材\\ppt\\2\\新的\\页面介绍展示模板\\");
//        String folderPath = "I:\\素材";
//
//        File directory = new File(folderPath);
//        if (!directory.isDirectory()) {
//            throw new FileNotFoundException('"' + folderPath + '"' + " 不是一个有效目录");
//        } else {
//            //首先将第一层目录扫描一遍
//            File[] files = directory.listFiles();
//            //遍历扫出的文件数组，如果是文件夹，将其放入到linkedList中稍后处理
//            for (int i = 0; i < files.length; i++) {
//                //是一个目录的时候
//                if (files[i].isDirectory()) {
//                    //删除该目录下所有jpg图片
//                    List<String> jpgs= FileScanHelper.scanFilesWithNoRecursion(files[i].getAbsolutePath(),false,"jpg");
//                    for(String jpg:jpgs)
//                    {
//                        File file=new File(jpg);
//                        file.delete();
//                    }
//                    handlePPtFlod(files[i].getAbsolutePath());
//                } else {
//                    //不是一个目录
//                }
//            }

//        }
    }
}

